/* Own includes */
#include "../include/charloader.h"
#include "../include/global.h"

/* Internal prototypes */
void char_load_sheets(xmlDocPtr charxml, xmlNodePtr node);

int char_load(char * characternumber)
{
    xmlDocPtr charxml = NULL;
    xmlNodePtr node = NULL;

    /* Assemble the xml filename */
    char * xmldocpath = malloc(sizeof(characternumber) + 23);
    memset(xmldocpath, '\0', sizeof(xmldocpath));
    strcat(xmldocpath, "xml/characters/char");
    strcat(xmldocpath, characternumber);
    strcat(xmldocpath, ".xml");

    /* Load the xml file */
    charxml = xml_loaddoc(xmldocpath);
    node = xml_getroot(charxml, "character");

    /* Load the spritesheets */
    char_load_sheets(charxml, node);

    /* Create a struct to keep the info about the char */
    indy = malloc(sizeof(*indy));
    indy->state = 0;
    indy->posx = 70;
    indy->posy = 135;
    indy->frameSizeX = 44;
    indy->frameSizeY = 63;
    indy->frameCur = 0;

    /* Free the no longer necessary file name and xml file */
    free(xmldocpath);
    xml_freedoc(charxml);

    return 0;
}

void char_load_sheets(xmlDocPtr charxml, xmlNodePtr node)
{
    /* Get the right node */
    node = xml_getnode(node, "graphics");

    /* Load the idle animation */
    char * sheet = xml_readnode(charxml, node, "image");

    /* Malloc space for scenebg image */
    imgChar = malloc(sizeof(*imgChar));

    /* Pass it the values */
    imgChar->img = load_bitmap(sheet, 0);
    imgChar->x = 0;
    imgChar->y = 0;

    /* Free the image path */
    free(sheet);
}

void char_animation()
{
    switch(indy->state)
    {
        case 0: /* Idle */
            masked_blit(imgChar->img, buffer, (indy->direction == 0 ? 44 : 0), 0, indy->posx - indy->frameSizeX / 2, indy->posy - indy->frameSizeY,
                indy->frameSizeX, indy->frameSizeY);
            break;
        case 1: /* Animation */
        {
            if(indy->direction == 0)
            {
                masked_blit(imgChar->img, buffer, indy->frameSizeX * indy->frameCur, indy->frameSizeY,
                    indy->posx - indy->frameSizeX / 2, indy->posy - indy->frameSizeY, indy->frameSizeX, indy->frameSizeY);
            }
            if(indy->direction == 1)
            {
                masked_blit(imgChar->img, buffer, indy->frameSizeX * indy->frameCur, indy->frameSizeY * 2,
                    indy->posx - indy->frameSizeX / 2, indy->posy - indy->frameSizeY, indy->frameSizeX, indy->frameSizeY);
            }
            if(indy->direction == 2)
            {
                masked_blit(imgChar->img, buffer, indy->frameSizeX * indy->frameCur, indy->frameSizeY * 3,
                    indy->posx - indy->frameSizeX / 2, indy->posy - indy->frameSizeY, indy->frameSizeX, indy->frameSizeY);
            }
            if(indy->direction == 3)
            {
                masked_blit(imgChar->img, buffer, indy->frameSizeX * indy->frameCur, indy->frameSizeY * 4,
                    indy->posx - indy->frameSizeX / 2, indy->posy - indy->frameSizeY, indy->frameSizeX, indy->frameSizeY);
            }
        } break;
    }
}

void char_timer(int speed)
{
    static int counter = 0;
    int framenum = 0;

    if(indy->direction <= 1)
    {
        framenum = 6;
    } else framenum = 5;

    if(indy->state == 1)
    {
        counter++;

        if(indy->frameCur == framenum && counter == speed)
        {
            indy->frameCur = 0;
            counter = 0;
        }

        if(counter == speed)
        {
            counter = 0;
            indy->frameCur++;
        }

    } else {
        indy->frameCur = 0;
        counter = 0;
    }
}

void char_cleanup()
{
    /* Image */
    destroy_bitmap(imgChar->img);
    free(imgChar);

    /* Char data */
    free(indy);
}
